package com.software.human.adapters;

import android.annotation.SuppressLint;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.software.human.R;
import com.software.human.entity.UserInfo;

import java.text.SimpleDateFormat;
import java.util.List;

public class UserInfoAdapter extends BaseAdapter {
    private Context context;
    private Integer layout;
    private List<UserInfo> userInfoList;

    public UserInfoAdapter(Context context, Integer layout, List<UserInfo> userInfoList) {
        this.context = context;
        this.layout = layout;
        this.userInfoList = userInfoList;
    }

    @Override
    public int getCount() {
        return userInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return userInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layout, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_user_name = convertView.findViewById(R.id.tv_user_name);
            viewHolder.tv_user_sex = convertView.findViewById(R.id.tv_user_sex);
            viewHolder.tv_user_birth = convertView.findViewById(R.id.tv_user_birth);
            viewHolder.tv_user_height = convertView.findViewById(R.id.tv_user_height);
            viewHolder.tv_user_weight = convertView.findViewById(R.id.tv_user_weight);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }



        UserInfo userInfo = userInfoList.get(position);
        viewHolder.tv_user_name.setText(userInfo.getUserName());
        viewHolder.tv_user_sex.setText(userInfo.getSex());
        viewHolder.tv_user_birth.setText(new SimpleDateFormat("yyyy-MM-dd").format(userInfo.getBirthday()));
        viewHolder.tv_user_height.setText(String.valueOf(userInfo.getHeight()));
        viewHolder.tv_user_weight.setText(String.valueOf(userInfo.getWeight()));
        return convertView;
    }

    public void loadMore(List<UserInfo> data) {
        this.userInfoList.addAll(data);
        notifyDataSetChanged();

    }

    static class ViewHolder {
        TextView tv_user_name;
        TextView tv_user_sex;
        TextView tv_user_birth;
        TextView tv_user_height;
        TextView tv_user_weight;

    }
    public void updateData(List<UserInfo> userInfoList) {
        this.userInfoList.clear();
        this.userInfoList.addAll(userInfoList);
        // 通知数据发生改变
        notifyDataSetChanged();
    }
}
